package chabernac.android.wordbattle;

import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Player;
import chabernac.android.wordbattle.model.Word;
import chabernac.android.wordbattle.model.WordRaster;
import chabernac.android.wordbattle.model.iWordRasterListener;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;

public class PlayerView extends View {
  private Picture myPicture;
  private Paint myPaint = new Paint();
  private int mySelectedPlayer;

  public PlayerView(Context aContext, AttributeSet aAttrs, int aDefStyle) {
    super(aContext, aAttrs, aDefStyle);
    init(aAttrs);
  }

  public PlayerView(Context aContext, AttributeSet aAttrs) {
    super(aContext, aAttrs);
    init(aAttrs);
  }

  public PlayerView(Context aContext) {
    super(aContext);
    init(null);
  }
  
  private void init(AttributeSet anAttributeSet){
    TypedArray theAttributes = getContext().obtainStyledAttributes(anAttributeSet,R.styleable.playerview);
    mySelectedPlayer = theAttributes.getInt( R.styleable.playerview_selectedPlayer, 0);
    theAttributes.recycle();
    
    SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.bar);
    myPicture = svg.getPicture();
    
    myPaint.setTextSize( 20 );
    myPaint.setColor( Color.WHITE );
    
    WordRasterContext.getInstance().getMediator().getWordRaster().addListener( new WordRasterListener() );
  }
  
  private Player getSelectedPlayer() {
    WordRaster theRaster = WordRasterContext.getInstance().getMediator().getWordRaster();
    
    if(theRaster.getPlayers().size() <= mySelectedPlayer) return null;
    return theRaster.getPlayers().toArray( new Player[]{} )[mySelectedPlayer];
  }

  public void setSelectedPlayer( int aSelectedPlayer ) {
    mySelectedPlayer = aSelectedPlayer;
  }

  @Override
  protected void onDraw(Canvas aCanvas) {
    Rect theTarget = new Rect( 0, 0, getWidth(), getHeight() );
    long t1 = System.currentTimeMillis();
    aCanvas.drawPicture(myPicture, theTarget);
    long t2 = System.currentTimeMillis();
    Log.d( PlayerView.class.getName(), "Rendering svg picture took '" + (t2 - t1) + "' ms");
    
    WordRaster theRaster = WordRasterContext.getInstance().getMediator().getWordRaster();
    int theScore = 0;
    Player thePlayer = getSelectedPlayer();
    if(thePlayer != null){
      theScore = theRaster.getScore( getSelectedPlayer() );
    }
    
    aCanvas.drawText( Integer.toString( theScore ), 150, 40,  myPaint);
  }
  
  public class WordRasterListener implements iWordRasterListener {

    @Override
    public void selectionChanged( List<Letter> aSelectedLetters ) {
//      postInvalidate();
    }

    @Override
    public void wordFound( Word aWord ) {
      postInvalidate();
    }
  }
}
